local skel = fk.CreateSkill {
  name = "emo__diyun",
  tags = { Skill.Rhyme  }
}

Fk:loadTranslationTable{
  ["emo__diyun"] = "笛韵",
  [":emo__diyun"] = "<a href='rhyme_skill'>韵律技</a>，回合技，平/仄：你可重铸若干张非“净”/非“灵”牌（若是本回合以此法重铸过的数量，〖笛韵〗本回合失效），视为使用一张名字数为X的基本或普通锦囊牌（X为你的“净”/“灵”数且至少为1）。"..
  "<br>转韵：发动〖净灵〗使用牌。",

  ["#emo__diyun_yang"] = "平：重铸若干张非“净”牌，视为使用一张 %arg 字牌",
  ["#emo__diyun_yin"] = "仄：重铸若干张非“灵”牌，视为使用一张 %arg 字牌",
  ["@emo__diyun-turn"] = "笛韵", -- 用于记录重铸牌的张数
}

---@param player Player
---@return number
local getJLnum = function (player)
  local mark = (player:getSwitchSkillState(skel.name) == fk.SwitchYang) and "@@EmoJingMark" or "@@EmoLingMark"
  return math.max(#table.filter(player:getCardIds("he"), function (id)
    return Fk:getCardById(id):getMark(mark) ~= 0
  end), 1)
end

skel:addEffect("viewas", {
  pattern = ".|.|.|.|.|basic,trick",
  prompt = function (self, player, selected_cards, selected)
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
      return "#emo__diyun_yang:::" .. getJLnum(player)
    else
      return "#emo__diyun_yin:::" .. getJLnum(player)
    end
  end,
  interaction = function (self, player)
    local num = getJLnum(player)
    local all_names = table.filter(Fk:getAllCardNames("bt"), function (name)
      local trueName = Fk:cloneCard(name).trueName
      return Fk:translate(trueName, "zh_CN"):len() == num
    end)
    local names = player:getViewAsCardNames(skel.name, all_names)
    if #names == 0 then return end
    return UI.CardNameBox { choices = names }
  end,
  card_filter = function (self, player, to_select, selected)
    local card = Fk:getCardById(to_select)
    if player:getSwitchSkillState(skel.name) == fk.SwitchYang then
      return card:getMark("@@EmoJingMark") == 0
    else
      return card:getMark("@@EmoLingMark") == 0
    end
  end,
  view_as = function (self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = skel.name
    card:setMark(skel.name, cards)
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local mark = use.card:getMark(skel.name)
    if type(mark) == "table" then
      room:recastCard(mark, player, skel.name)
      if not room:addTableMarkIfNeed(player, "@emo__diyun-turn", #mark) then
        room:invalidateSkill(player, skel.name, "-turn")
      end
    end
  end,
  enabled_at_play = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryTurn) == 0
  end,
  times = function (self, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryTurn)
  end,
  enabled_at_response = function (self, player, response)
    if not response and Fk.currentResponsePattern and player:usedSkillTimes(skel.name, Player.HistoryTurn) == 0 then
      local num = getJLnum(player)
      for _, name in ipairs(Fk:getAllCardNames("bt")) do
        local trueName = Fk:cloneCard(name).trueName
        if Fk:translate(trueName, "zh_CN"):len() == num and Exppattern:Parse(Fk.currentResponsePattern):matchExp(".|.|.|.|"..trueName) then
          return true
        end
      end
    end
  end,
})

return skel
